3 Easy Ways to Find IP address in Linux 您所在的位置:网站首页 terminal ip address command 3 Easy Ways to Find IP address in Linux

3 Easy Ways to Find IP address in Linux

2023-03-11 02:46| 来源: 网络整理| 查看: 265

An IP address is a unique identifier assigned to a computer or device connected to the internet.

In this blog post, we will discuss three methods to find the IP address of a Linux system: using the ifconfig command, using the ip command, and using the ip route command.

Let’s get started!

 

 

Procedures to find ip address in Linux Open the terminal application. Type ifconfig -a or ip addr or ip route command Press Enter to run the command. The output will display the ip address for all the network interfaces.

 

Related: Step by Step Guide to troubleshoot a network issue in Linux

Find IP address with ip addr command in Linux

The most convenient way to find ip address in Linux is using ip addr command.  All we need is to open the terminal then type ip addr in the prompt and press enter.

The number next to inet is the IP address. This command will list IP address, MAC address, MTU size and other information about a network interface.

I find this command very useful, as it gives me more detailed information about my system’s IP address and the status of the network interface.

Furthermore, we can also type ip address or ip a for short.

ip addr – Show IP address for all interfaces ip addr show dev em1 – Display information only for device em1 ip addr show up – Show IP address for the running interfaces

 

By default, the ip addr command will display ip information of all the network interfaces on the Linux system.

This is the command output in my system.

# ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever

2: eth0: mtu 9001 qdisc mq state UP group default qlen 1000 link/ether 02:3f:4d:eb:52:e7 brd ff:ff:ff:ff:ff:ff inet 10.1.0.143/24 brd 10.1.0.255 scope global dynamic noprefixroute eth0 valid_lft 3198sec preferred_lft 3198sec

3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff inet 10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0 valid_lft 70000sec preferred_lft 70000sec

From the above example, we can see that there are three network interfaces l0, eth0 and eth1.

The ip address for eth0 is 10.1.0.143 and the ip address for eth1 is 10.254.222.37.

This output is easily understood.

The inet entry displays the IP address for the interface.  UP signifies that the interface is ready for use;  Brd denotes that the interface is connected to a network that supports broadcast messaging (ethernet) LOOPBACK shows which device (lo) is the loopback address. The maximum transmission unit (MTU) on eth0 is 1500 bytes. This determines the size of the largest packet that can be transmitted over this interface (and is sometimes “tuned” to other values for performance enhancement).

 

 

If you have more than one network interface on your system, you can use ip addr show dev command to view the IP address of the specific interface.

The syntax for this command is:

ip addr show dev interface_name

Let’s say if I want to find the ip address of interface eth1, I will use the following command:

$ ip addr show dev eth1 3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether fa:16:3e:18:3a:76 brd ff:ff:ff:ff:ff:ff inet 10.254.222.37/23 brd 10.254.223.255 scope global dynamic eth0 valid_lft 69786sec preferred_lft 69786sec

Additionally, I would like to share another command ip addr show up.

The “ip addr show up” command is used to display the network interfaces that are currently in an “up” state, which means that they are enabled and active.

There are a number of other options that you can use with the ip addr command. For more information, you can type command ip addr help.

To learn more about ip command, I would recommend the following article.

3 useful Linux commands you need to know about networking

Get Ip address with ifconfig command in Linux

The ifconfig command is a common command that is used to view IP related information in Linux. The command output includes the interface name, its assigned IP address, netmask, broadcast address, and other network-related information.

If no argument is given, this command displays the configuration details of all active network interfaces on the system.

In addition to that, if you are only interested in certain interface eth0, use: ifconfig eth0

To use this command, just type ifconfig in the shell prompt and press enter.

# ifconfig -a eth0: flags=4163 mtu 9001 inet 10.1.0.143 netmask 255.255.255.0 broadcast 10.1.0.255 ether 02:3f:4d:eb:52:e7 txqueuelen 1000 (Ethernet) RX packets 1142453 bytes 77488923 (73.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5835535 bytes 8505814381 (7.9 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 

The ifconfig command is a powerful network configuration utility that can be used to display or configure a network interface. 

Furthermore, it should be noted that ifconfig command is considered as deprecated command and it is replaced by iproute2 package in many distributions.

This means that it may not be supported in newer versions of the operating system and it may not be compatible with some distributions.

If you need to install it, you can follow this guide.

3 Ways to fix ifconfig command not found in Linux

Related: 10 Linux Interview Questions about networking You Need to Know

Get Ip address with ip route command in Linux

The ip route command can be used to find the IP address of a Linux system.

This command will print out the IP address of our system along with other network details. Once we execute the command we will see an output similar to the text below.

$ ip route default via 10.1.0.1 dev eth0 proto dhcp metric 100 10.1.0.0/24 dev eth0 proto kernel scope link src 10.1.0.143 metric 100

The information shows us the server’s IP routing table and network gateway.

On these lines, the IP address following `src` are the IP(s) configured on the server.

In the example above, we can see that the server has an IP address of `10.1.0.143`.

Check IP address with Bash script in Linux

We can get a list of the network interfaces and IPv4 addresses on our server by running the following command:

ip -4 -o a | cut -d ' ' -f 2,7 | cut -d '/' -f 1

The output lists the interface names on the left and the associated IP addresses on the right.

lo 127.0.0.1 eth0 10.1.0.79

Let’s break down the command:

ip -4 -o a : output each record on a single line, replacing line feeds with the ‘\’ character. This is convenient when you want to get ip address in bash script.

The cut command is used to extract a subset of the lines from a command output, or to extract a subset of the columns.

The “-d” parameter specifies the delimiter that is used to separate the fields.

The “-f” parameter specifies the field number(s) that you want to extract.

Here’s an example: cut -d’,’ -f1,3 filename.txt This command will extract the first and third column from the lines in the file filename.txt, using commas as the delimiter.

Related: Bash Shell Script to Find IP address

Find public IP address in Linux

The way to get your public IP address is to Google it. Type in “what is my IP address” or “how to find my IP address,” and Google will tell you.

Your IP address is public to the internet. We can get it from many online tools. It can be easily tracked by others. 

Alternatively, you can try using curl to access an external service that will detect your public IP. There are quite a few out there – I just went with the first search result I found:

curl ifconfig.me/all

ip_addr: 64.104.44.110 remote_host: unavailable user_agent: curl/7.84.0 port: 35116 language: referer: connection: keep_alive: method: GET encoding: mime: */* charset: via: 1.1 google forwarded: 64.104.44.110, 34.160.111.145,35.191.8.3%

There’s another way to determine your public IP address. Perform a DNS lookup against myip.opendns.com. It’s a service provided by OpenDNS and it’s lightening fast!

dig +short myip.opendns.com @resolver1.opendns.com 64.104.44.110

 

What is Ip address?

From Wikipedia, IP address is used in a system for communication. It is a numerical label such as 192.0.2.1. An IP address serves two main functions: network interface identification and location addressing.

An IP address is the identifier that enables our device to send or receive data packets across the internet. It holds information related to our location and therefore making devices available for two-way communication.

Most internet service providers still use IPv4. It’s based on 32 binary bits, consists of four numbers from 0 to 255, and is separated by dots. For example, 103.86.98.1.

A Linux server can have more than one network interface. This is often the case with web servers and other devices that need to be able to connect to different networks. For example, a web server may need to be able to connect to both the internet and a local network.

A network interface can have more than one IP address. The first address is called the primary address, while the others are called secondary addresses or aliases. Using secondary IP addresses is common with web servers.

For example, a web server may have a primary IP address that is used to connect to the internet and a secondary IP address that is used to connect to a local network. This allows the server to be reachable from both networks.

In the following example, there are two IP addresses 10.252.10.51 and 10.252.10.52 on the physical interface eth0.

eth0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7 inet addr:10.252.10.51 Bcast:10.252.10.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:50:56:8F:44:C7 inet addr:10.252.10.52 Bcast:10.252.10.255 Mask:255.255.255.0

 

3 Ways to Find MAC Address In Linux

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有